﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;

namespace CJJCH.Web
{
    public partial class NewsDynamic : System.Web.UI.Page
    {
        CJJCH.BLL.T_NET_MenuSecond bll_second = new CJJCH.BLL.T_NET_MenuSecond();
        CJJCH.BLL.T_NET_MenuThird bll_third = new CJJCH.BLL.T_NET_MenuThird();
        CJJCH.BLL.T_NET_News bll_news = new CJJCH.BLL.T_NET_News();
        CJJCH.BLL.T_NET_PicNews bll_picnews = new BLL.T_NET_PicNews();
        public string strPic, strLink, strText;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitRequest();
                MenuBind();
                rptNewsDataBind();
            }
        }
        private void InitRequest()
        {
            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"].ToString()))
            {
                ViewState["secondid"] = Request.QueryString["secondid"].ToString();
            }
            else
            {
                ViewState["secondid"] = bll_second.GetMinModel(1).ID.ToString();
            }
            if (Request.QueryString["thirdid"]!=null&&LTP.Common.CheckInput.CheckDigit(Request.QueryString["thirdid"].ToString()))
            {
                ViewState["thirdid"] = Request.QueryString["thirdid"].ToString();
            }
            else
            {
                CJJCH.Model.T_NET_MenuThird model= bll_third.GetMinModel(Convert.ToInt32(ViewState["secondid"].ToString()));
                ViewState["thirdid"] = model.ID.ToString();
                
            }
            if (Request.QueryString["name"] != null )
            {

                this.divTitle.InnerHtml = Request.QueryString["name"].ToString();
            }
            else
            {
                this.divTitle.InnerHtml = "通知公告";
            }
            if (Request.QueryString["curpage"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["curpage"].Trim()))
            {
                AspNetPager1.CurrentPageIndex = System.Convert.ToInt32(Request.QueryString["curpage"].Trim());
                AspNetPager2.CurrentPageIndex = AspNetPager1.CurrentPageIndex;
            }
            else
            {
                AspNetPager1.CurrentPageIndex = 1;
                AspNetPager2.CurrentPageIndex = AspNetPager1.CurrentPageIndex;
            }
        }

        private void MenuBind()
        {
            IList<CJJCH.Model.T_NET_MenuSecond> list = new List<CJJCH.Model.T_NET_MenuSecond>();
            list = bll_second.GetMenuSecondList(0, 1);
            this.rptMenu.DataSource = list;
            this.rptMenu.DataBind();
        }

        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            ((HyperLink)e.Item.FindControl("lnkMenu")).Text = ((CJJCH.Model.T_NET_MenuSecond)e.Item.DataItem).MenuName;
            Repeater rptMenu = (Repeater)e.Item.FindControl("rptSubMenu");
            IList<CJJCH.Model.T_NET_MenuThird> list = new List<CJJCH.Model.T_NET_MenuThird>();
            list = bll_third.GetThird(((CJJCH.Model.T_NET_MenuSecond)e.Item.DataItem).ID,"");
            if (list.Count>0)
            {
                rptMenu.DataSource = list;
                rptMenu.DataBind();
            }
        }

        protected void rptSubMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                ((HyperLink)e.Item.FindControl("lnkSubMenu")).Text = ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).MenuName;
                ((HyperLink)e.Item.FindControl("lnkSubMenu")).NavigateUrl = "NewsDynamic.aspx?secondid="
                    + ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).SecondID.ToString()
                    + "&thirdid=" + ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString()
                    + "&name=" + ((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).MenuName;
                if (((CJJCH.Model.T_NET_MenuThird)e.Item.DataItem).ID.ToString()==ViewState["thirdid"].ToString())
                {
                    HtmlGenericControl liSubMenu = (HtmlGenericControl)e.Item.FindControl("liSubMenu");
                    liSubMenu.Attributes.Add("class", "SubmenuCur");
                }
            }
        }

        private void rptNewsDataBind()
        {
            #region 绑定图片新闻

            DataSet ds = null;
            string PicUploadPath = LTP.Common.ConfigHelper.GetConfigString("UploadPic").Replace("~/", "");
            ds = bll_picnews.GetPicNews();
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (!dr["PicUrl"].ToString().Contains("://"))
                    {
                        strPic += dr["PicUrl"].ToString().Replace("~/", "") + "|";
                    }
                    else
                    {
                        strPic += dr["PicUrl"].ToString() + "|";
                    }
                    strLink += dr["HrefUrl"].ToString() + "|";
                    if (dr["Instructions"].ToString().Length > 10)
                    {
                        strText += dr["Instructions"].ToString().Substring(0, 10) + "…|";
                    }
                    else
                    {
                        strText += dr["Instructions"].ToString() + "|";
                    }
                }
            }

            strPic = string.IsNullOrEmpty(strPic) ? "" : strPic.Remove(strPic.Length - 1, 1).ToString();
            strLink = string.IsNullOrEmpty(strLink) ? "" : strLink.Remove(strLink.Length - 1, 1).ToString();
            strText = string.IsNullOrEmpty(strText) ? "" : strText.Remove(strText.Length - 1, 1).ToString();
            #endregion

            int totalCount = 0;
            IList<CJJCH.Model.T_NET_News> list = new List<CJJCH.Model.T_NET_News>();
            list = bll_news.GetNewsList(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, Convert.ToInt32(ViewState["secondid"].ToString()), "", Convert.ToInt32(ViewState["thirdid"].ToString()), "", "", ref totalCount, "");
            this.rptNews.DataSource = list;
            this.rptNews.DataBind();
            AspNetPager1.RecordCount = totalCount;
            AspNetPager2.RecordCount = totalCount;
            if (list.Count == 0)
                litMessage.Text = "<table width=100%><tr style=height:18px bgcolor=#FFFFFF><td align='center'><font color=red>对不起，系统没有检索出有关信息！<font></td></tr></table>";
            else
                litMessage.Text = "";

            AspNetPager1.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>条记录";
            AspNetPager1.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager1.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
            AspNetPager2.CustomInfoHTML = "共有：<font color=\"blue\"><b>" + AspNetPager1.RecordCount.ToString() + "</b></font>条记录";
            AspNetPager2.CustomInfoHTML += " <font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
            AspNetPager2.CustomInfoHTML += "/<font color=\"blue\"><b>" + AspNetPager1.PageCount.ToString() + "</b></font>";
        }

        protected void rptNews_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            { 
                ((HyperLink)e.Item.FindControl("lnkTitle")).Text =LTP.Common.StringClass.GetENString(((CJJCH.Model.T_NET_News)e.Item.DataItem).DataTitle,20);
                ((HyperLink)e.Item.FindControl("lnkTitle")).NavigateUrl = "NewsDetail.aspx?ID="
                    + LTP.Common.StringClass.EncodeUrl(((CJJCH.Model.T_NET_News)e.Item.DataItem).ID.ToString())
                    + "&curpage=" + AspNetPager1.CurrentPageIndex.ToString()
                    + "";
                ((Label)e.Item.FindControl("lblPlusDate")).Text = ((CJJCH.Model.T_NET_News)e.Item.DataItem).PlusDate.ToString("yyyy/MM/dd");
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptNewsDataBind();
            AspNetPager2.CurrentPageIndex = AspNetPager1.CurrentPageIndex;
        }
        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = AspNetPager2.CurrentPageIndex;
        }
    }
}